package com.xmh.factoryDemo.factory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import com.xmh.factoryDemo.service.UserService;

@Component
@PropertySource(value = "classpath:factory.properties")
public class StrategyFactory implements ApplicationContextAware {

	@Value("${FactoryUser.ImplClassName}")
	private String userServiceImplType;

	private static UserService userService;

	public static UserService getUserService() {
		return userService;
	}

	private void setUserService(UserService userService) {
		StrategyFactory.userService = userService;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		setUserService((UserService) applicationContext.getBean(userServiceImplType));

//		applicationContext.getBeansOfType(UserService.class).values().forEach(e -> {
//			if (e.getClass().getName().equals(userServiceImplType))
//				userService = e;
//		});
	}

}
